/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package almacenamiento;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Iterator;
/**
 *
 * @author Usuario
 */
public class Demo {
    public static void main(String args[]) {
        String n1="";
        String n2="";
        try {
        List<Atributo> attrs = new ArrayList<Atributo>();
        List<Comparable> v = new ArrayList<Comparable>();

        System.out.println("Ingrese cuantas tuplas desea ingresar");
        Scanner entrada = new Scanner(System.in); //Aqui declaramos el Scanner
        Scanner tupla = new Scanner(System.in);
        int nroTuplas=entrada.nextInt();
        System.out.println("Nro de atributos es:"+ nroTuplas);
        System.out.println("ahora ingrese las nombres ");
        for(int i=1;i<=nroTuplas;i++)
        {
           System.out.println("Tupla "+ i+":");
            String cadena=entrada.next();
            //System.out.println("string:"+cadena);
            if(cadena=="character")
            {
                attrs.add(new Atributo(cadena, Character.class));
                System.out.println(cadena+": ");
                char c=tupla.next().charAt(2);
                
                v.add(new Character(c));
            }
            if(cadena=="byte")
            {
                attrs.add(new Atributo(cadena, Byte.class));
                System.out.println(cadena+": ");
                byte b=tupla.nextByte();
                v.add(new Byte((byte) b));
            }
            if(cadena=="short")
            {
                attrs.add(new Atributo(cadena,Short.class ));
                System.out.println(cadena+": ");
                short s=tupla.nextShort();
                v.add(new Short((short) s));
            }
            if(cadena=="integer")
            {
                attrs.add(new Atributo(cadena,Integer.class ));
                System.out.println(cadena+": ");
                int ii=tupla.nextInt();
                v.add(new Integer(ii));
            }
            if(cadena=="long")
            {
                attrs.add(new Atributo(cadena, Long.class));
                System.out.println(cadena+": ");
                long l=tupla.nextLong();
                v.add(new Long(l));
            }
            if(cadena=="float")
            {
                attrs.add(new Atributo(cadena, Float.class));
                System.out.println(cadena+": ");
                float f=tupla.nextFloat();
                v.add(new Float(f));
            }
            if(cadena=="double")
            {
                attrs.add(new Atributo(cadena,Double.class ));
                System.out.println(cadena+": ");
                double f=tupla.nextDouble();
                v.add(new Double(25.6));
            }
            if(cadena=="string")
            {
                attrs.add(new Atributo(cadena, String.class));
                System.out.println(cadena+": ");
                String st=tupla.next();
                v.add(new String(st));
            }
        }
        
            Relacion rel = new Relacion(attrs);
            Tupla t = new Tupla(new IdentificadorTupla("redes", 0), v);
            GestorIOTupla man = new GestorIOTupla(rel, "redes");
            java.io.RandomAccessFile raf = new java.io.RandomAccessFile("redes", "rw");

            System.out.println("writing tuple..." + t);
            byte[] bytes = new byte[1024];
            //man.escribirTupla(t, raf);
            man.escribirTupla(t, bytes, 0);
            raf.close();
            System.out.println("mostrar arreglo de bytes: "+bytes);
            raf = new java.io.RandomAccessFile("redes", "r");
            System.out.println("reading tuple...");
            // t = man.leerTupla(bytes);
            Par<Tupla, Integer> st = man.leerTupla(bytes, 0);
            System.out.println(st);
            raf.close();
        }catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }
    /*try {
            List<Atributo> attrs = new ArrayList<Atributo>();
            attrs.add(new Atributo("character", Character.class));
            attrs.add(new Atributo("byte", Byte.class));
            attrs.add(new Atributo("short", Short.class));
            attrs.add(new Atributo("integer", Integer.class));
            attrs.add(new Atributo("long", Long.class));
            attrs.add(new Atributo("float", Float.class));
            attrs.add(new Atributo("double", Double.class));
            attrs.add(new Atributo("string", String.class));
            Relacion relacion = new Relacion(attrs);

            //para agregar una tuplas
            Random r = new  Random();
            int n = r.nextInt(100);
            Tupla t;
            GestorIOTupla man = new GestorIOTupla(relacion, "redes");

            RandomAccessFile raf = new RandomAccessFile("redes", "rw");
            byte[] bytes = new byte[15000];
            int k =0;
            int l =0;
            int [] des= new int[n];
            
            
            for (int i = 0; i < n; i++) {
                List<Comparable> v = new ArrayList<Comparable>();
                v.add(new Character('a'));
                v.add(new Byte((byte) 26));
                v.add(new Short((short) 312));
                v.add(new Integer(2048));
                v.add(new Long(34567));
                v.add(new Float(12.3));
                v.add(new Double(25.6));
                v.add(new String("bla bla"));
                t = new Tupla(new IdentificadorTupla("redes", l), v);
                System.out.println("writing tuple..." + t);
                //man.escribirTupla(t, raf);
                k = man.escribirTupla(t, bytes, k);
                l++;
                des[i] = k;
            }
             raf.close();

             
             
            raf = new java.io.RandomAccessFile("redes", "r");
            System.out.println("reading tuple...");
            // t = man.leerTupla(bytes);
            
            Integer y=new Integer(0);
            int g = 0;
            
            for (int i = 0; i < n; i++) {
                Par<Tupla, Integer> st = man.leerTupla(bytes, des[g]);
                System.out.println(st.toString());
              //y = st.segundo.intValue();
              System.out.println("");
              g++;            
    
               }
           //man.toString();
            raf.close();
            
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }*/
    }
}
